﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime;
using System.ServiceModel;
using Zen4SyncOrchestratorServices;

namespace Zen4SyncSSISComponent
{
    [DtsTask( DisplayName = "Zen4Sync Duplicate Relative TestSession Task", Description = "Zen4Sync Duplicate Relative TestSession Task", TaskType = "DTS100" )]
    public class DuplicateRelativeTestSessionTask : Task
    {
       public string TestSessionId
        {
            get;
            set;
        }

        public int Seconds
        {
            get;
            set;
        }

        public string NewTestSessionId { get; set; }
        public string NewTestSessionName { get; set; }
        public string NewTestSessionCreatedOn { get; set; }


        public override DTSExecResult Execute(
            Connections connections,
            VariableDispenser variableDispenser,
            IDTSComponentEvents componentEvents,
            IDTSLogging log,
            object transaction )
        {   
            ChannelFactory<IOrchestratorServices> client = new ChannelFactory<IOrchestratorServices>( "Config" );

            IOrchestratorServices orchestrator = client.CreateChannel();
            orchestrator.DuplicateTestSessionRelative( new Guid(TestSessionId), Seconds, new Guid(NewTestSessionId),NewTestSessionName, DateTime.Parse(NewTestSessionCreatedOn));

            client.Close();
            return DTSExecResult.Success;
        }

        public override DTSExecResult Validate(
            Connections connections,
            VariableDispenser variableDispenser,
            IDTSComponentEvents componentEvents,
            IDTSLogging log )
        {
            if(string.IsNullOrEmpty( TestSessionId ))
                return DTSExecResult.Success;

            try
            {
                Guid guid = new Guid( TestSessionId );
                return DTSExecResult.Success;
            }
            catch
            {
                return DTSExecResult.Failure;
            }

        }

        
    }
}

